﻿using System;
using System.Collections;
using System.Data;
using System.Web.UI.HtmlControls;
using DotNetNuke.Framework;
using gdlmo.DNN.Modules.Article.Data;
using DotNetNuke.Entities.Users;

namespace gdlmo.DNN.Modules.Article.Business
{
	public class ArticleController : DotNetNuke.Entities.Modules.ISearchable, DotNetNuke.Entities.Modules.IPortable
	{
		#region "Public Methods"		
		public ArticleInfo Get(int ArticleID)
		{
			return (ArticleInfo)DotNetNuke.Common.Utilities.CBO.FillObject(DataProvider.Instance().GetArticle(ArticleID), typeof(ArticleInfo));
		}

		public ArrayList List()
		{
			return DotNetNuke.Common.Utilities.CBO.FillCollection(DataProvider.Instance().ListArticle(), typeof(ArticleInfo));
		}

		public ArrayList GetByCategory(int CategoryId )
		{
			return DotNetNuke.Common.Utilities.CBO.FillCollection(DataProvider.Instance().GetArticleByCategory(CategoryId), typeof(ArticleInfo));
		}
	

		public int Add(ArticleInfo objArticle,System.Web.HttpFileCollection files)
		{
			ArrayList fileslist = new ArrayList();
			string ArticleNames ="";
			if(gdlmoGlobal.SaveFiles(files,"UploadFile",fileslist))
			{
				for(int i=0;i<fileslist.Count;i++)
				{
					ArticleNames += fileslist[i];
					ArticleNames += ";";
				}
				return (int)DataProvider.Instance().AddArticle(objArticle.CategoryId, objArticle.Title, objArticle.CreatedDate, objArticle.CreatedByUser, objArticle.CopyFrom, objArticle.Author, objArticle.Hits, objArticle.IsPassed, objArticle.IP, objArticle.DeptName,objArticle.Summary,ArticleNames);	
			}
			else return -1;
		}
		public void Update(ArticleInfo objArticle)
		{					
			DataProvider.Instance().UpdateArticle(objArticle.ArticleID, objArticle.CategoryId, objArticle.Title, objArticle.CreatedDate, objArticle.CreatedByUser, objArticle.CopyFrom, objArticle.Author, objArticle.Hits, objArticle.IsPassed, objArticle.IP, objArticle.DeptName,objArticle.Summary);			
		}
		
		public void Update(ArticleInfo objArticle,System.Web.HttpFileCollection files)
		{
			ArrayList fileslist = new ArrayList();
			AccesoryController objCtlAccesory = new AccesoryController();
			
			ArrayList lsAccesory = objCtlAccesory.List(objArticle.ArticleID);
			for(int i=0;i<lsAccesory.Count;i++)
			{
				fileslist.Add(((AccesoryInfo)lsAccesory[i]).ArticleName);
			}
			gdlmoGlobal.DeleteFile("UploadFile",fileslist);	
			
			string ArticleNames ="";
			if(gdlmoGlobal.SaveFiles(files,"UploadFile",fileslist))
			{
				for(int i=0;i<fileslist.Count;i++)
				{
					ArticleNames += fileslist[i];
					ArticleNames += ";";
				}
				DataProvider.Instance().UpdateArticle(objArticle.ArticleID, objArticle.CategoryId, objArticle.Title, objArticle.CreatedDate, objArticle.CreatedByUser, objArticle.CopyFrom, objArticle.Author, objArticle.Hits, objArticle.IsPassed, objArticle.IP, objArticle.DeptName,objArticle.Summary,ArticleNames);
			}
			else throw new Exception("更新文章出错，请联系管理员");
		}
		public void Delete(int ArticleID)
		{
			DataProvider.Instance().DeleteArticle(ArticleID);
		}	
		public ArrayList GetPagingData(int PageIndex,int PageSize,ref int PageCount)
		{
			ArrayList list = new ArrayList();
			IDataReader dr = DataProvider.Instance().GetPageData(PageIndex,PageSize);
			dr.Read();
			int Rcount = Convert.ToInt32(dr[0]);
			dr.NextResult();
			list = DotNetNuke.Common.Utilities.CBO.FillCollection(dr, typeof(ArticleInfo));
			if ((Rcount % PageSize) > 0)
			{
				PageCount = (Rcount / PageSize) + 1;
			}
			else
			{
				PageCount = Rcount / PageSize;
			}			
			return list;
//			return DotNetNuke.Common.Utilities.CBO.FillCollection(DataProvider.Instance().GetPageData(PageIndex,PageSize,ref PageCount), typeof(ArticleInfo));
		}
		public ArrayList GetPagingData(int CategoryId,int PageIndex,int PageSize,ref int PageCount)
		{
			ArrayList list = new ArrayList();
			IDataReader dr = DataProvider.Instance().GetPageData(CategoryId,PageIndex,PageSize);
			dr.Read();
			int Rcount = Convert.ToInt32(dr[0]);
			dr.NextResult();
			list = DotNetNuke.Common.Utilities.CBO.FillCollection(dr, typeof(ArticleInfo));
			if ((Rcount % PageSize) > 0)
			{
				PageCount = (Rcount / PageSize) + 1;
			}
			else
			{
				PageCount = Rcount / PageSize;
			}			
			return list;
//			return DotNetNuke.Common.Utilities.CBO.FillCollection(DataProvider.Instance().GetPageData(CategoryId,PageIndex,PageSize,ref PageCount), typeof(ArticleInfo));
		}		
		public ArrayList GetPagingData(int CategoryId,string Title,string StartCreatedDate, string EndCreatedDate , string Author , string DeptName , string Summary,int PageIndex,int PageSize,ref int PageCount)
		{			
			ArrayList list = new ArrayList();
            using (IDataReader dr = DataProvider.Instance().GetPageData(CategoryId, Title, StartCreatedDate, EndCreatedDate, Author, DeptName, Summary, PageIndex, PageSize))
            {
                dr.Read();
                int Rcount = Convert.ToInt32(dr[0]);
                dr.NextResult();
                list = DotNetNuke.Common.Utilities.CBO.FillCollection(dr, typeof(ArticleInfo));
                if ((Rcount % PageSize) > 0)
                {
                    PageCount = (Rcount / PageSize) + 1;
                }
                else
                {
                    PageCount = Rcount / PageSize;
                }
                return list;
            }
//			return DotNetNuke.Common.Utilities.CBO.FillCollection(DataProvider.Instance().GetPageData(CategoryId,Title , StartCreatedDate, EndCreatedDate , Author , DeptName , Summary,PageIndex,PageSize,ref PageCount), typeof(ArticleInfo));
		}	
		public ArrayList GetPagingData(ArticleInfo objArticle,int PageIndex,int PageSize,ref int PageCount)
		{
			int CategoryId;
			string Title ,StartCreatedDate ,EndCreatedDate ,Author ,DeptName ,Summary;
			CategoryId = objArticle.CategoryId;
			Title = objArticle.Title;
			StartCreatedDate = "";
			EndCreatedDate   = "";			//此时这两个属性没有使用
			Author  = objArticle.Author;
            DeptName = objArticle.DeptName;
			Summary = objArticle.Summary;
			ArrayList list = new ArrayList();
			IDataReader dr = DataProvider.Instance().GetPageData(CategoryId,Title , StartCreatedDate, EndCreatedDate , Author , DeptName , Summary,PageIndex,PageSize);
			dr.Read();
			int Rcount = Convert.ToInt32(dr[0]);
			dr.NextResult();
			list = DotNetNuke.Common.Utilities.CBO.FillCollection(dr, typeof(ArticleInfo));
			if ((Rcount % PageSize) > 0)
			{
				PageCount = (Rcount / PageSize) + 1;
			}
			else
			{
				PageCount = Rcount / PageSize;
			}			
			return list;
//			return DotNetNuke.Common.Utilities.CBO.FillCollection(DataProvider.Instance().GetPageData(CategoryId,Title , StartCreatedDate, EndCreatedDate , Author , DeptName , Summary,PageIndex,PageSize,ref PageCount), typeof(ArticleInfo));
		}
		public ArrayList GetPagingData(ArticleInfo objArticle1,ArticleInfo objArticle2,int PageIndex,int PageSize,ref int PageCount)
		{
			int CategoryId;
			string Title ,StartCreatedDate ,EndCreatedDate ,Author ,DeptName ,Summary;
			CategoryId = objArticle1.CategoryId;
			Title = objArticle1.Title;
			if(objArticle1.CreatedDate.Year>1)
			{
				StartCreatedDate = objArticle1.CreatedDate.ToString();
				EndCreatedDate   = objArticle2.CreatedDate.ToString();
			}
			else
			{
				StartCreatedDate="";
				EndCreatedDate="";

			}
			Author  = objArticle1.Author;
			DeptName = objArticle1.DeptName;
			Summary = objArticle1.Summary;

			ArrayList list = new ArrayList();
			IDataReader dr = DataProvider.Instance().GetPageData(CategoryId,Title , StartCreatedDate, EndCreatedDate , Author , DeptName , Summary,PageIndex,PageSize);
			dr.Read();
			int Rcount = Convert.ToInt32(dr[0]);
			dr.NextResult();
			list = DotNetNuke.Common.Utilities.CBO.FillCollection(dr, typeof(ArticleInfo));
			if ((Rcount % PageSize) > 0)
			{
				PageCount = (Rcount / PageSize) + 1;
			}
			else
			{
				PageCount = Rcount / PageSize;
			}			
			return list;
//			return DotNetNuke.Common.Utilities.CBO.FillCollection(DataProvider.Instance().GetPageData(CategoryId,Title , StartCreatedDate, EndCreatedDate , Author , DeptName , Summary,PageIndex,PageSize,ref PageCount), typeof(ArticleInfo));
		}
		/// <summary>
		/// 返回总记录数
		/// </summary>
		/// <returns>总记录数据</returns>
		public int GetRcount()
		{			
			return DataProvider.Instance().GetArticleCount();	
		}
		/// <summary>
		/// 返回指定分类的总记录数
		/// </summary>
		/// <param name="CategoryId">分类编号</param>
		/// <returns>指定分类的总记录数</returns>
		public int GetRcount(int CategoryId)
		{
			return DataProvider.Instance().GetArticleCount(CategoryId);
		}
		
		public int ArticleSigned(int ArticleId,int UserId,DateTime SignedDate)
		{
			return DataProvider.Instance().AddArticleSigned(ArticleId,UserId,SignedDate);
		}
		public ArrayList GetArticleUnSigned(int UserId)
		{			
			return DotNetNuke.Common.Utilities.CBO.FillCollection(DataProvider.Instance().GetArticleUnSigned(UserId), typeof(ArticleInfo));
		}
		public ArrayList GetArticleUnsignedUsers(int PortalId,int ArticleId)
		{
			DotNetNuke.Security.Roles.RoleController objCtlRols = new DotNetNuke.Security.Roles.RoleController();			
			int RoleId = ((DotNetNuke.Security.Roles.RoleInfo)objCtlRols.GetRoleByName(PortalId,"Signed")).RoleID;
			return DotNetNuke.Common.Utilities.CBO.FillCollection(DataProvider.Instance().GetUnsignedUsers(PortalId,RoleId,ArticleId), typeof(UserInfo));
		}
        /// <summary>
        /// 取文档的管理模块的编号
        /// </summary>
        /// <param name="PortalId">门户编号</param>
        /// <returns>ModuleInfo的List数组</returns>
        /// <remarks>在最早的1.0版中只需要“return DotNetNuke.Common.Utilities.CBO.FillCollection(DataProvider.Instance().GetArticleManagerModules(PortalId), typeof(DotNetNuke.Entities.Modules.ModuleInfo))”即可，
        /// 但DNN5.0后可能ModuleInfo的属性已有变化，取不到值</remarks>
		public ArrayList GetArticleManagerModules(int PortalId)
		{
            ArrayList list = new ArrayList();
            using (IDataReader dr = DataProvider.Instance().GetArticleManagerModules(PortalId))
            {
                dr.Read();
                DotNetNuke.Entities.Modules.ModuleInfo mdInfo = new DotNetNuke.Entities.Modules.ModuleInfo();
                mdInfo.ModuleTitle = dr["ModuleTitle"].ToString(); ;
                mdInfo.ModuleID = (int)dr["ModuleID"];                
                list.Add(mdInfo);                
                return list;
            }
            //return DotNetNuke.Common.Utilities.CBO.FillCollection(DataProvider.Instance().GetArticleManagerModules(PortalId), typeof(DotNetNuke.Entities.Modules.ModuleInfo));
		}
		#endregion

		#region "Optional Interfaces"		
        public DotNetNuke.Services.Search.SearchItemInfoCollection GetSearchItems(DotNetNuke.Entities.Modules.ModuleInfo ModInfo)
		{
//			DotNetNuke.Services.Search.SearchItemInfoCollection infos2 = new DotNetNuke.Services.Search.SearchItemInfoCollection();
//			ArrayList articles = this.GetArticles(ModInfo.ModuleID);
//			foreach (ArticleInfo info in articles)
//			{
//				string str2;
//				ArticleInfo info3 = info;
//				int nullInteger = DotNetNuke.Common.Utilities.Null.NullInteger;
//				if (Information.IsNumeric(info3.CreatedByUser))
//				{
//					nullInteger = int.Parse(info3.CreatedByUser);
//				}
//				string content = HttpUtility.HtmlDecode(info3.Title + " " + info3.KeyWords);
//				if (StringType.StrCmp(info3.Summary.Trim(), "", false) == 0)
//				{
//					str2 = DotNetNuke.Common.Utilities.HtmlUtils.Shorten(DotNetNuke.Common.Utilities.HtmlUtils.Clean(HttpUtility.HtmlDecode(info3.Content), false), 100, "...");
//				}
//				else
//				{
//					str2 = DotNetNuke.Common.Utilities.HtmlUtils.Shorten(DotNetNuke.Common.Utilities.HtmlUtils.Clean(HttpUtility.HtmlDecode(info3.Summary), false), 100, "...");
//				}
//				DotNetNuke.Services.Search.SearchItemInfo info2 = new DotNetNuke.Services.Search.SearchItemInfo(ModInfo.ModuleTitle + " - " + info3.Title, str2, nullInteger, info3.CreatedDate, ModInfo.ModuleID, info3.ArticleID.ToString(), content, "mid=" + ModInfo.ModuleID.ToString() + "&ctl=ArticleShow&ArticleId=" + info3.ArticleID.ToString(), DotNetNuke.Common.Utilities.Null.NullInteger);
//				infos2.Add(info2);
//				info3 = null;
//			}
//			return infos2;
			return null;
		}
	
        public string ExportModule(int ModuleID)
		{
			return "";
        }

        public void ImportModule(int ModuleID, string Content, string Version, int UserId)
		{
		}
		#endregion
	}
}
